package com.example.smartall.utils;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * @Author: abbas
 * @Description: sp工具类
 */
public class SPUtils {
    private static SharedPreferences preferences;
    private static SharedPreferences.Editor editor;
    private static SPUtils spUtils = new SPUtils();

    //    public SPUtils getInstance(){
//        if (spUtils == null){
//            spUtils = new SPUtils();
//        }
//        return spUtils;
//    }
    private SPUtils() {

    }

    //每次拿token都要
    // new SPUtils(Context,Constant.xxx).getString(xxx)
    @Deprecated
    public SPUtils(Context context, String fileName) {
        preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
        editor = preferences.edit();

    }

    public static SPUtils config(Context context, String fileName) {
        preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
        editor = preferences.edit();
        return spUtils;
    }

    //清除所有数据
    public void clearAll() {
        editor.clear();
        editor.commit();
    }

    //判断是否存在token
    public Boolean hasToken() {
        if (!"".equals(preferences.getString(Constant.TOKEN, "")))
            return true;
        return false;
    }

    //判断是否存在端口以及ip
    public Boolean hasSet() {
        if (!"".equals(preferences.getString(Constant.NET_PORT, "")) && !"".equals(preferences.getString(Constant.NET_IP, "")))
            return true;
        return false;
    }

    //获取一个String
    public String getString(String key) {
        if ("".equals(preferences.getString(key, ""))) return "";
        return preferences.getString(key, "");
    }

    //写入一个String
    public void putString(String key, String value) {
        editor.putString(key, value);
        editor.commit();
    }
    //其他写入或获取可以自行添加
}
